<?php
namespace App\Http\Controllers\Admin;

use App\Models\SystemSetting;
use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class IndexController extends BaseController
{


    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index()
    {
        /*$att= '很高=2,较高=1,一般=0,较低=-1,很低=-2';
        $reward= '很高=2,较高=1,一般=0,较低=-1,很低=-2';
        $punish= '很高=2,较高=1,一般=0,较低=-1,很低=-2';
        $feedback = '交流能力:高级=3,中级=2,低级=1
沟通能力:高级=3,中级=2,低级=1
学习能力:高级=3,中级=2,低级=1
积极能力:高级=3,中级=2,低级=1
考试能力:高级=3,中级=2,低级=1';
        $logo = 'images/ads2.png';
        $name = '青苜';
        $slogan = '青苜是个好地方';
        

        $set = new SystemSetting();
        $set->varname = '公司名称';
        $set->value = $name;
        $set->app = 'name';
        $set->is_set = '1';
        $set->save();

        $set = new SystemSetting();
        $set->varname = '公司口号';
        $set->value = $slogan;
        $set->app = 'slogan';
        $set->is_set = '1';
        $set->save();

        $set = new SystemSetting();
        $set->varname = '公司logo';
        $set->value = $logo;
        $set->app = 'logo';
        $set->is_set = '1';
        $set->save();

        //考勤
        $att_array = explode(',',$att);
        $at = array();
        foreach ($att_array as $aa){
            $b = explode('=',$aa);
            $at[$b[0]] = $b[1];
        }
        $set = new SystemSetting();
        $set->varname = '考勤';
        $set->value = serialize($at);
        $set->app = 'attendence';
        $set->is_set = '1';
        $set->save();

        //奖励
        $reward_array = explode(',',$reward);
        $re = array();
        foreach ($reward_array as $aa){
            $b = explode('=',$aa);
            $re[$b[0]] = $b[1];
        }
        $set = new SystemSetting();
        $set->varname = '奖励';
        $set->value = serialize($re);
        $set->app = 'reward';
        $set->is_set = '1';
        $set->save();

        //惩罚
        $punish_array = explode(',',$punish);
        $pu = array();
        foreach ($punish_array as $aa){
            $b = explode('=',$aa);
            $pu[$b[0]] = $b[1];
        }
        $set = new SystemSetting();
        $set->varname = '惩罚';
        $set->value = serialize($pu);
        $set->app = 'punish';
        $set->is_set = '1';
        $set->save();

        //月度测评
        $array = explode(PHP_EOL, $feedback);
        $fd = array();
        $b = array();
        foreach ($array as $a){
            $b = explode(':',$a);
            $fd[trim($b[0])] = $b[1];
        }
        foreach ($fd as $k=>$f){
            $f_array = explode(',',$f);
            $pu = array();
            foreach ($f_array as $aa){
                $b = explode('=',$aa);
                $pu[$b[0]] = $b[1];
            }
            $set = new SystemSetting();
            $set->varname = $k;
            $set->value = serialize($pu);
            $set->app = 'ceping';
            $set->is_set = '1';
            $set->save();
        }
        dd('1');*/
        return view('admin.kaohe.init.add');
    }


    //编辑公司名称等
    public function editinit()
    {
        $name = SystemSetting::where('app','name')->first();
        $slogan = SystemSetting::where('app','slogan')->first();
        $logo = SystemSetting::where('app','logo')->first();
        return view('admin.kaohe.init.edit')
            ->with('name',$name)
            ->with('slogan',$slogan)
            ->with('logo',$logo);
    }
    
    
    //修改公司信息存储
    public function postEditinit(Request $req)
    {
        $logo = $req->input('logo');
        $name = $req->input('name');
        $slogan = $req->input('slogan');
        if(empty($logo)||empty($name)||empty($slogan)){
            return Response::json(array('status'=>'fail','msg'=>'公司信息未填写完整！'));
        }

        $set = SystemSetting::where('app','name')->first();
        $set->value = $name;
        $set->save();

        $set2 = SystemSetting::where('app','slogan')->first();
        $set2->value = $slogan;
        $set2->save();

        $set3 = SystemSetting::where('app','logo')->first();
        $set3->value = $logo;
        $set3->save();
        return Response::json(array('status'=>'success','msg'=>'保存成功！','redirectUrl'=>'/admin/user'));
        
    }
   

    /**
     * 初始化存储
     */
    public function init(Request $req)
    {
        $logo = $req->input('logo');
        $name = $req->input('name');
        $slogan = $req->input('slogan');
        if(empty($logo)||empty($name)||empty($slogan)){
            return Response::json(array('status'=>'fail','msg'=>'公司信息未填写完整！'));
        }
        $att = $req->input('attendence');
        if(empty($att)){
            return Response::json(array('status'=>'fail','msg'=>'考勤信息未填写完整！'));
        }
        $reward = $req->input('reward');
        if(empty($reward)){
            return Response::json(array('status'=>'fail','msg'=>'奖励等级分数未填写完整！'));
        }
        $punish = $req->input('punish');
        if(empty($punish)){
            return Response::json(array('status'=>'fail','msg'=>'惩罚等级分数未填写完整！'));
        }
        $feedback = $req->input('feedback');
        if(empty($feedback)){
            return Response::json(array('status'=>'fail','msg'=>'月度测评等级分数未填写完整！'));
        }

        DB::beginTransaction();
        try{
            $set = new SystemSetting();
            $set->varname = '公司名称';
            $set->value = $name;
            $set->app = 'name';
            $set->is_set = '1';
            $set->save();

            $set = new SystemSetting();
            $set->varname = '公司口号';
            $set->value = $slogan;
            $set->app = 'slogan';
            $set->is_set = '1';
            $set->save();

            $set = new SystemSetting();
            $set->varname = '公司logo';
            $set->value = $logo;
            $set->app = 'logo';
            $set->is_set = '1';
            $set->save();

            //考勤
            $att_array = explode('@',$att);
            $att_array =  array_filter($att_array);
            $at = array();
            foreach ($att_array as $aa){
                $b = explode('=',$aa);
                $at[$b[0]] = $b[1];
            }
            $set = new SystemSetting();
            $set->varname = '考勤';
            $set->value = serialize($at);
            $set->app = 'attendence';
            $set->is_set = '1';
            $set->save();

            //奖励
            $reward_array = explode('@',$reward);
            $reward_array =  array_filter($reward_array);
            $re = array();
            foreach ($reward_array as $aa){
                $b = explode('=',$aa);
                $re[$b[0]] = $b[1];
            }
            $set = new SystemSetting();
            $set->varname = '奖励';
            $set->value = serialize($re);
            $set->app = 'reward';
            $set->is_set = '1';
            $set->save();

            //惩罚
            $punish_array = explode('@',$punish);
            $punish_array =  array_filter($punish_array);
            $pu = array();
            foreach ($punish_array as $aa){
                $b = explode('=',$aa);
                $pu[$b[0]] = $b[1];
            }
            $set = new SystemSetting();
            $set->varname = '惩罚';
            $set->value = serialize($pu);
            $set->app = 'punish';
            $set->is_set = '1';
            $set->save();

            //月度测评
            $array = explode('#', $feedback);
            $array=  array_filter($array);
            $fd = array();
            $b = array();
            foreach ($array as $a){
                $b = explode('%',$a);
                //dd($b);
                $fd[trim($b[0])] = $b[1];
            }
            foreach ($fd as $k=>$f){
                $f_array = explode('@',$f);
                $pu = array();
                foreach ($f_array as $aa){
                    $b = explode('=',$aa);
                    $pu[$b[0]] = $b[1];
                }
                $set = new SystemSetting();
                $set->varname = $k;
                $set->value = serialize($pu);
                $set->app = 'ceping';
                $set->is_set = '1';
                $set->save();
            }
            DB::commit();
            return Response::json(array('status'=>'success','msg'=>'保存成功！','redirectUrl'=>'/admin/user'));
        }catch (\PDOException $e){
            DB::rollback();
            Log::info(['初始化出错'=>$e->getMessage()]);
            return Response::json(array('status'=>'fail','msg'=>'初始化失败！请检查输入格式'));
        }
    }

    /**
     * 修改密码
     */
    public function  modifyPwd(Request $req)
    {
        if(!$this->user->can('editOwnerInfo')) abort(405);
        
        $data = $req->only('oldpwd','newpwd','newpwd_repeat','uid');
        if(($data['uid'] == $this->user->id) && $data['uid'] > 0)
        {
            $userPwd = User::find($data['uid']);
            if($userPwd->password !== $data['oldpwd'])
            {
                return redirect()->intended('admin/index/modifypwd')->withErrors(['oldpassword'=>'原密码输入错误']);
            }
            if($data['newpwd'] !== $data['newpwd_repeat'])
            {
                return redirect()->intended('admin/index/modifypwd')->withErrors(['newpassword'=>'两次密码输入不一致，请检查后重新输入']);
            }
            $userPwd->password = $data['newpwd'];
            $userPwd->save();
            return redirect()->intended('admin/index/modifypwd');
        }
        else
        {
            return view('admin.pwd');
        }
    }
    
    public function postImg(Request $request)
    {
        $img = $request->input('dataURL','');
        if(empty($img)){
            return Response::json(array('status'=>'fail','msg'=>'图像不能为空！'));
        }
        if (strstr($img,",")){
            $img = explode(',',$img);
            $img = $img[1];
        }
        $imageName = "user".date("His",time())."_".rand(1111,9999).'.png';
        $logo = "images/kaohe/". $imageName;
        $r = file_put_contents(public_path($logo), base64_decode($img));
        if (!$r) {
            return Response::json(array('status'=>'fail','msg'=>'图像上传失败！'));
        }else{
            return Response::json(array('status'=>'success','data'=>$logo,'msg'=>'头像上传成功！'));
        }
    }
}

